﻿using System;
using Com.Lancetinc.Evs.Data;
using Com.Lancetinc.Evs.Helpers;
using Com.Lancetinc.Evs.ViewModels;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;

namespace Com.Lancetinc.Evs.Controls
{
    /// <summary>
    /// 历史数据查询条件
    /// </summary>
    public partial class HistoryQueryCondUserControl
    {
        private readonly QueryCondViewModel QueryCond = new();

        public HistoryQueryCondUserControl()
        {
            QueryCond.SexItems.Add(new KeyValuePair<string, string>("", ""));
            QueryCond.SexItems.Add(new KeyValuePair<string, string>("1", I18nHelper.Default.GetMessage("Male")));
            QueryCond.SexItems.Add(new KeyValuePair<string, string>("2", I18nHelper.Default.GetMessage("Female")));

            InitializeComponent();

            this.ReloadSelectItems();
            this.DataContext = QueryCond;
        }

        public void ReloadSelectItems()
        {
            // AttendingDoctor
            ComboBoxSelectItemsHelper.InitializeItems(QueryCond.AttendingDoctorItems, Properties.Settings.Default.AttendingDoctorItems);

            // Mediastinus
            ComboBoxSelectItemsHelper.InitializeItems(QueryCond.MediastinusItems, Properties.Settings.Default.MediastinusItems);
        }

        public void ClearInputConds()
        {
            QueryCond.SeqNo = string.Empty;
            QueryCond.MedicalRecordNo = string.Empty;
            QueryCond.BedNo = string.Empty;
            QueryCond.Name = string.Empty;
            QueryCond.Sex = string.Empty;
            QueryCond.AgeBegin = null;
            QueryCond.AgeEnd = null;
            QueryCond.AttendingDoctor = string.Empty;
            QueryCond.Mediastinus = string.Empty;
            QueryCond.Notes = string.Empty;
            QueryCond.Identity = string.Empty;
            QueryCond.Diagnosis = string.Empty;
            QueryCond.PrintStatus = 0;
            QueryCond.TimeStart = string.Empty;
            QueryCond.TimeEnd = string.Empty;
        }

        public SqlCondItem[] GetInputConds()
        {
            List<SqlCondItem> conds = new List<SqlCondItem>();
            if (!string.IsNullOrEmpty(QueryCond.SeqNo))
            {
                conds.Add(new SqlCondItem("SeqNo", QueryCond.SeqNo, "like"));
            }
            if (!string.IsNullOrEmpty(QueryCond.MedicalRecordNo))
            {
                conds.Add(new SqlCondItem("MedicalRecordNo", QueryCond.MedicalRecordNo, "like"));
            }
            if (!string.IsNullOrEmpty(QueryCond.BedNo))
            {
                conds.Add(new SqlCondItem("BedNo", QueryCond.BedNo, "like"));
            }
            if (!string.IsNullOrEmpty(QueryCond.Name))
            {
                conds.Add(new SqlCondItem("Name", QueryCond.Name, "like"));
            }
            if (!string.IsNullOrEmpty(QueryCond.Sex))
            {
                conds.Add(new SqlCondItem("Sex", QueryCond.Sex, "="));
            }
            if (QueryCond.AgeBegin.HasValue)
            {
                conds.Add(new SqlCondItem("Age", QueryCond.AgeBegin, ">="));
            }
            if (QueryCond.AgeEnd.HasValue)
            {
                conds.Add(new SqlCondItem("Age", QueryCond.AgeEnd, "<="));
            }
            if (!string.IsNullOrEmpty(QueryCond.AttendingDoctor))
            {
                conds.Add(new SqlCondItem("AttendingDoctor", QueryCond.AttendingDoctor, "like"));
            }
            if (!string.IsNullOrEmpty(QueryCond.Mediastinus))
            {
                conds.Add(new SqlCondItem("Mediastinus", QueryCond.Mediastinus, "like"));
            }
            if (!string.IsNullOrEmpty(QueryCond.Notes))
            {
                conds.Add(new SqlCondItem("Notes", QueryCond.Notes, "like"));
            }
            if (!string.IsNullOrEmpty(QueryCond.Diagnosis))
            {
                conds.Add(new SqlCondItem("Diagnosis", QueryCond.Diagnosis, "like"));
            }
            if (!string.IsNullOrEmpty(QueryCond.Identity))
            {
                conds.Add(new SqlCondItem("Identity", QueryCond.Identity, "like"));
            }
            if (QueryCond.PrintStatus > 0)
            {
                // 未打印
                if (QueryCond.PrintStatus == 1)
                {
                    conds.Add(new SqlCondItem("PrintCount", 0, "="));
                }
                else if (QueryCond.PrintStatus == 2)
                {
                    // 已打印
                    conds.Add(new SqlCondItem("PrintCount", 0, ">"));
                }
            }

            DateTime? videoStartTime = null;
            DateTime? videoEndTime = null;

            if (!string.IsNullOrEmpty(QueryCond.TimeStart))
            {
                videoStartTime = DateUtil.ToDate(QueryCond.TimeStart);
            }
            if (!string.IsNullOrEmpty(QueryCond.TimeEnd))
            {
                videoEndTime = DateUtil.ToDate(QueryCond.TimeEnd);
            }

            if (videoStartTime != null)
            {
                var startTimeStr = videoStartTime.Value.ToString(DateUtil.DateFormatStrYyyyMmDdHhMmSs);
                conds.Add(new SqlCondItem("StartTime", startTimeStr, ">="));
                conds.Add(new SqlCondItem("EndTime", startTimeStr, ">="));
            }

            if (videoEndTime != null)
            {
                // 如果结束时间是日期没时间，则使用当天的最晚时间(23:59:59)
                if (videoEndTime.Value == videoEndTime.Value.Date)
                {
                    videoEndTime = DateUtil.GetEndOfDate(videoEndTime.Value);
                }
                var endTimeStr = videoEndTime.Value.ToString(DateUtil.DateFormatStrYyyyMmDdHhMmSs);
                conds.Add(new SqlCondItem("StartTime", endTimeStr, "<="));
                conds.Add(new SqlCondItem("EndTime", endTimeStr, "<="));
            }

            return conds.ToArray();
        }

        public Task<bool> ValidInput()
        {
            if (!string.IsNullOrEmpty(QueryCond.TimeStart))
            {
                if (DateUtil.ToDate(QueryCond.TimeStart) == null)
                {
                    var msg = I18nHelper.Default.GetMessageWithArgs("XIsInvalid", I18nHelper.Default.GetMessage("Exper.VideoTimeRangeStart"));
                    MessageHelper.ShowError(msg);
                    return Task.FromResult(false);
                }
            }

            if (!string.IsNullOrEmpty(QueryCond.TimeEnd))
            {
                if (DateUtil.ToDate(QueryCond.TimeEnd) == null)
                {
                    var msg = I18nHelper.Default.GetMessageWithArgs("XIsInvalid", I18nHelper.Default.GetMessage("Exper.VideoTimeRangeEnd"));
                    MessageHelper.ShowError(msg);
                    return Task.FromResult(false);
                }
            }

            return Task.FromResult(true);
        }
    }

    class QueryCondViewModel : ViewModelBase
    {
        private string mSeqNo;
        public string SeqNo
        {
            get => mSeqNo;
            set
            {
                string trimmedString = value?.Trim();
                if (mSeqNo != trimmedString)
                {
                    mSeqNo = trimmedString;
                    RaisePropertyChanged();
                }
            }
        }

        private string mMedicalRecordNo;
        public string MedicalRecordNo
        {
            get => mMedicalRecordNo;
            set
            {
                string trimmedString = value?.Trim();
                if (mMedicalRecordNo != trimmedString)
                {
                    mMedicalRecordNo = trimmedString;
                    RaisePropertyChanged();
                }
            }
        }

        private string mBedNo;
        public string BedNo
        {
            get => mBedNo;
            set
            {
                string trimmedString = value?.Trim();
                if (mBedNo != trimmedString)
                {
                    mBedNo = trimmedString;
                    RaisePropertyChanged();
                }
            }
        }

        private string mName;
        public string Name
        {
            get => mName;
            set
            {
                string trimmedString = value?.Trim();
                if (mName != trimmedString)
                {
                    mName = trimmedString;
                    RaisePropertyChanged();
                }
            }
        }

        private string mDiagnosis;
        /// <summary>
        /// 诊断
        /// </summary>
        public string Diagnosis
        {
            get => mDiagnosis;
            set
            {
                string trimmedString = value?.Trim();
                if (mDiagnosis != trimmedString)
                {
                    mDiagnosis = trimmedString;
                    RaisePropertyChanged();
                }
            }
        }

        private string mIdentity;
        /// <summary>
        /// 身份证
        /// </summary>
        public string Identity
        {
            get => mIdentity;
            set
            {
                string trimmedString = value?.Trim();
                if (mIdentity != trimmedString)
                {
                    mIdentity = trimmedString;
                    RaisePropertyChanged();
                }
            }
        }

        private string mTimeStart;
        /// <summary>
        /// 开始时间
        /// </summary>
        public string TimeStart
        {
            get => mTimeStart;
            set
            {
                string trimmedString = value?.Trim();
                if (mTimeStart != trimmedString)
                {
                    mTimeStart = trimmedString;
                    RaisePropertyChanged();
                }
            }
        }

        private string mTimeEnd;
        /// <summary>
        /// 结束时间
        /// </summary>
        public string TimeEnd
        {
            get => mTimeEnd;
            set
            {
                string trimmedString = value?.Trim();
                if (mTimeEnd != trimmedString)
                {
                    mTimeEnd = trimmedString;
                    RaisePropertyChanged();
                }
            }
        }

        private int mPrintStatus;
        public int PrintStatus
        {
            get => mPrintStatus;
            set => SetProperty(ref mPrintStatus, value);
        }

        private string mSex;
        public string Sex
        {
            get => mSex;
            set => SetProperty(ref mSex, value);
        }

        private int? mAgeBegin;
        public int? AgeBegin
        {
            get => mAgeBegin;
            set => SetProperty(ref mAgeBegin, value);
        }

        private int? mAgeEnd;
        public int? AgeEnd
        {
            get => mAgeEnd;
            set => SetProperty(ref mAgeEnd, value);
        }

        private string mAttendingDoctor;
        public string AttendingDoctor
        {
            get => mAttendingDoctor;
            set
            {
                string trimmedString = value?.Trim();
                if (mAttendingDoctor != trimmedString)
                {
                    mAttendingDoctor = trimmedString;
                    RaisePropertyChanged();
                }
            }
        }

        private string mMediastinus;
        public string Mediastinus
        {
            get => mMediastinus;
            set
            {
                string trimmedString = value?.Trim();
                if (mMediastinus != trimmedString)
                {
                    mMediastinus = trimmedString;
                    RaisePropertyChanged();
                }
            }
        }

        private string mNotes;
        public string Notes
        {
            get => mNotes;
            set
            {
                string trimmedString = value?.Trim();
                if (mNotes != trimmedString)
                {
                    mNotes = trimmedString;
                    RaisePropertyChanged();
                }
            }
        }

        private ObservableCollection<KeyValuePair<string, string>> mSexItems = new();
        public ObservableCollection<KeyValuePair<string, string>> SexItems => mSexItems;

        private ObservableCollection<string> mAttendingDoctorItems = new();
        public ObservableCollection<string> AttendingDoctorItems => mAttendingDoctorItems;

        private ObservableCollection<string> mMediastinusItems = new();
        public ObservableCollection<string> MediastinusItems => mMediastinusItems;

        private static string[] _reportStateItems;
        public string[] ReportStateItems
        {
            get
            {
                if (_reportStateItems != null)
                    return _reportStateItems;
                _reportStateItems = new[]
                {
                    "",
                    I18nHelper.Default.GetMessage("ReportState.NotPrinted"),
                    I18nHelper.Default.GetMessage("ReportState.Printed"),
                };
                return _reportStateItems;
            }
        }
    }
}
